{% extends 'public/main.tpl' %}
{% block stylesheets %}
    <link rel="stylesheet" href="/stylesheets/adduser.css">
{% endblock %}

{% block content %}
    <section class="box">
        <a href="/listUser" class="add-user"> 返回用户列表 >> </a>
        <form class="list" method="post" onsubmit="return submitform();"  autocomplete="off">
            <input type="hidden" name="userId" value="{{data[0].id|default(0)}}"/>
            <div class="list-item">
                <input type="text" placeholder="用户姓名" name="username"  value="{{data[0].name|default(data[0].name)}}"/>
            </div>
            <div class="list-item">
                <input type="tel" placeholder="手机号" name="phone" maxlength="11" value="{{data[0].phone|default(data[0].phone)}}"/>
            </div>
            <div class="list-item">
                <input type="text" placeholder="密码" name="psd" value="{{data[0].password|default(data[0].password)}}"/>
            </div>
            <div class="list-item">
                <select name="role">
                    <option value ="1">管理员</option>
                    <option value ="2">销售</option>
                </select>
            </div>
            <button type="submit" > 保存 </button>
        </form>
    </section>
{% endblock %}

{% block script %}
<script>

    function submitform() {
        $.ajax({
            type: "post",
            url: "/api/adduser",
            data: $("form").serialize(),
            dataType: "json",
            success: function (res) {
                console.log(res)
                if(res.status == 200) {
                    alert(res.msg)
                    setTimeout(window.location.href = "/listUser" ,2000)
                } else{
                    alert(res.msg)
                }
            }
        });
        return false
    }
</script>
{% endblock %}